package com.listsorter;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

/**
 * 
 * 
 * @author tamerhur
 */
public class ReflectionUtil
{

   public static Object invokeObject(String methods[], Object target, Object[] args, Class<?>... parameterTypes) throws Exception
   {
      Object result = target;
      try
      {
         for (String name : methods)
         {
            if (!name.equalsIgnoreCase(""))
            {
               Method metod = result.getClass().getMethod(name, parameterTypes);
               result = metod.invoke(result, args);
            }
         }
      }
      catch (IllegalArgumentException iae)
      {
         throw new IllegalArgumentException(iae);
      }
      catch (NoSuchMethodException nsme)
      {
         throw new NoSuchMethodException(nsme.getMessage());
      }
      catch (InvocationTargetException ite)
      {
         throw ite;
      }
      return result;
   }

   public static Object invokeObject(String method, Object target, Object[] args, Class<?>... parameterTypes) throws Exception
   {
      Object result = target;
      try
      {
         Method metod = result.getClass().getMethod(method, parameterTypes);
         result = metod.invoke(result, args);
      }
      catch (IllegalArgumentException iae)
      {
         throw new IllegalArgumentException(iae);
      }
      catch (NoSuchMethodException nsme)
      {
         throw new NoSuchMethodException(nsme.getMessage());
      }
      catch (InvocationTargetException ite)
      {
         throw ite;
      }
      return result;
   }

   public static String invokeAndAppend(String methods[], Object target, Object[] args, Class<?>... parameterTypes) throws Exception
   {
      Object result = null;
      StringBuffer sb = new StringBuffer();
      try
      {
         if (null == target)
         {
            return "";
         }
         for (String name : methods)
         {
            Method metod = target.getClass().getMethod(name, parameterTypes);
            result = metod.invoke(target, args);
            if (result == null)
               sb.append("");
            else
               sb.append(result.toString());
         }
      }
      catch (IllegalArgumentException iae)
      {
         throw new IllegalArgumentException(iae);
      }
      catch (NoSuchMethodException nsme)
      {
         throw new NoSuchMethodException(nsme.getMessage());
      }
      catch (InvocationTargetException ite)
      {
         throw ite;
      }
      return sb.toString();
   }

   public static Method getMethod(String methods[], Object target, Object[] args, Class<?>... parameterTypes) throws Exception
   {
      Object result = target;
      Method metod = null;
      try
      {
         for (String name : methods)
         {
            if (!name.equalsIgnoreCase(""))
            {
               metod = result.getClass().getMethod(name, parameterTypes);
               result = metod.invoke(result, args);
            }
         }
      }
      catch (IllegalArgumentException iae)
      {
         throw new IllegalArgumentException(iae);
      }
      catch (NoSuchMethodException nsme)
      {
         throw new NoSuchMethodException(nsme.getMessage());
      }

      return metod;
   }

   public static List<Method> getGetterMethods(Object arg)
   {
      ArrayList<Method> methods = new ArrayList<Method>();
      for (Method m : arg.getClass().getMethods())
      {
         if (m.getName().startsWith("get"))
         {
            methods.add(m);
         }
      }
      return methods;
   }

}